//
//  MYGMyFollowsTableViewCell.swift
//  MYDoctor
//
//  Created by Mo on 2024/3/7.
//

import UIKit

class MYGMyFollowsTableViewCell: UITableViewCell {
    var headerImageV:UIImageView!
    var nameLabel:UILabel!
    var contentLab:UILabel!
    var followBtn:UIButton!
    var tapFollowCompletion:(()->Void)?
    
    func chatFollow() {
        followBtn.backgroundColor = RGBA(r: 245, g: 245, b: 245, a: 1)
        followBtn.setTitleColor(RGBA(r: 87, g: 87, b: 87, a: 1), for: .normal)
        self.followBtn.setTitle("发私信", for: .normal)
    }
    func followed() {
        followBtn.backgroundColor = RGBA(r: 245, g: 245, b: 245, a: 1)
        followBtn.setTitleColor(RGBA(r: 87, g: 87, b: 87, a: 1), for: .normal)
        self.followBtn.setTitle("取消关注", for: .normal)
    }
    
    func backFollowed() {
        followBtn.backgroundColor = UIColor.init(hexString: "#FF4C6C")
        followBtn.setTitleColor(.white, for: .normal)
        self.followBtn.setTitle("回关", for: .normal)
    }
    var model = MYGFriendsModel()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        headerImageV = UIImageView()
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAvatar))
        headerImageV.isUserInteractionEnabled = true
        headerImageV.addGestureRecognizer(tap)
        headerImageV.layer.masksToBounds = true
        headerImageV.contentMode = .scaleAspectFill
        headerImageV.layer.cornerRadius = SCALEWIDTH(25)
        contentView.addSubview(headerImageV)
        headerImageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(15))
            make.centerY.equalToSuperview()
            make.width.height.equalTo(SCALEWIDTH(50))
        }
        
        nameLabel = UILabel()
        nameLabel.textColor = RGBA(r: 43, g: 43, b: 43, a: 1)
        nameLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        nameLabel.text = "医生名字"
        self.contentView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(headerImageV.snp.top)
            make.left.equalTo(headerImageV.snp.right).offset(SCALEWIDTH(12.5))
            make.height.equalTo(SCALEWIDTH(22.5))
        }
        
        contentLab = UILabel()
        contentLab.textColor = RGBA(r: 128, g: 128, b: 128, a: 1)
        contentLab.font = UIFont.systemFont(ofSize: 13, weight: .regular)
        contentLab.text = "医院名字"
        self.contentView.addSubview(contentLab)
        contentLab.snp.makeConstraints { make in
            make.bottom.equalTo(headerImageV.snp.bottom)
            make.left.equalTo(headerImageV.snp.right).offset(SCALEWIDTH(12.5))
            make.height.equalTo(SCALEWIDTH(18.5))
        }
        
        followBtn = UIButton()
        followBtn.setTitle("已关注", for: .normal)
        followBtn.setTitleColor(RGBA(r: 87, g: 87, b: 87, a: 1), for: .normal)
        followBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        followBtn.backgroundColor = RGBA(r: 245, g: 245, b: 245, a: 1)
        followBtn.layer.masksToBounds = true
        followBtn.layer.cornerRadius = SCALEWIDTH(3)
        followBtn.addTarget(self, action: #selector(tapFollowbtn), for: .touchUpInside)
        contentView.addSubview(followBtn)
        followBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(SCALEWIDTH(-15))
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: SCALEWIDTH(60), height: SCALEWIDTH(23)))
        }
    }
    
    func bindData(_ data: MYGFriendsModel) {
        self.model = data
        self.headerImageV.sd_setImage(with: URL(string: data.from_doctor.avatar), placeholderImage: UIImage.init(named: "head"), context: nil)
        self.nameLabel.text = data.from_doctor.name
        self.contentLab.text = data.from_doctor.department
        if model.type == 1 {
            chatFollow()
        }else {
            if data.is_mutual == 1 {
                followed()
            }else {
                backFollowed()
            }
        }
       
    }
    
    @objc func tapFollowbtn() {
        if self.followBtn.titleLabel?.text == "取消关注" {
            //取消关注
            MYGLoading.show()
            MMYGNetwork<MYGCommonAPI,MYGEmptyModel>.request(target: .unfollowAction(id: "\(self.model.from_doctor.id)", prefix: "followUpDoctor")) { _ in
                MYGLoading.hide()
                if let c = self.tapFollowCompletion {
                    c()
                }
            }  failed: { _, error in
                MYGLoading.hide()
                MMYGHUD.showToast(text: error)
            }
        }
        if self.followBtn.titleLabel?.text == "回关" {
            //关注
            MYGLoading.show()
            MMYGNetwork<MYGCommonAPI,MYGEmptyModel>.request(target: .followAction(id: "\(self.model.from_doctor.id)", prefix: "followUpDoctor")) { _ in
                MYGLoading.hide()
                if let c = self.tapFollowCompletion {
                    c()
                }
            }  failed: { _, error in
                MYGLoading.hide()
                MMYGHUD.showToast(text: error)
            }
        }
        if self.followBtn.titleLabel?.text == "发私信" {
            let phone = "\( self.model.from_doctor.phone)1"
            let vc = MYGChatController(sessionId: 0,mobile:phone,title: self.model.from_doctor.name)
            self.rootResponsder().navigationController?.pushViewController(vc)
        }
        
    }
    
    @objc func tapAvatar() {
        let vc = MYGMicrostationViewController()
        vc.model = self.model
        self.rootResponsder().navigationController?.pushViewController(vc, animated: true)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
